<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dell queen
 * Date: 12/28/12
 * Time: 8:40 PM
 * To change this template use File | Settings | File Templates.
 */
class Upload extends Front_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper('thumb');

    }
    public function index(){
        $this->config->load('uploading_script');
        $upload_dir = $this->config->item('upload_dir');
        $primary_dir = $this->config->item('primary_dir');
        $secondary_dir = $this->config->item('secondary_dir');

        if(count($this->userdata)){
            $folder = 'products/'.$this->userdata['id'];
        }else {
            $folder='products/guest';
        }

        // Create Primary Directory if it does not exist
        if(! is_dir('./' . $upload_dir . '/' . $folder . '/'))
        {
            mkdir('./' . $upload_dir . '/' . $folder . '/');
        }
        // Configure Upload class
        $config['upload_path'] = $upload_dir . '/' . $folder . '/';
        $config['allowed_types'] = $this->config->item('acceptable_files');
        $config['max_size']	= 1024*2;
        $arr = $_FILES['userfile']['type'];
        if($arr=='image/png'){
            $ext = ".png";
        }else if($arr=='image/jpeg'){
            $ext = ".jpg";
        }else {
            $ext = ".jpg";
        }
        $config['file_name'] = 'product_'.time().$ext;
        $this->load->library('upload', $config);;
        // Output json as response
        if ( ! $this->upload->do_upload())
        {
            $json['status'] = 'error';
            $json['issue'] = $this->upload->display_errors('','');
        }
        else
        {
            $json['status'] = 'success';
            foreach($this->upload->data() as $k => $v)
            {
                $json[$k] = $v;
            }
            $json['file_url']= $config['upload_path'].$config['file_name'];
        }
        echo json_encode($json);
        exit();
    }

    public function avatar(){
        $this->config->load('uploading_script');
        $upload_dir = $this->config->item('upload_dir');
        $primary_dir = $this->config->item('primary_dir');
        $secondary_dir = $this->config->item('secondary_dir');
        $folder = 'avatar/'.$this->userdata['id'];

        // Create Primary Directory if it does not exist
        if(! is_dir('./' . $upload_dir . '/' . $folder . '/'))
        {
            mkdir('./' . $upload_dir . '/' . $folder . '/');
        }
        // Configure Upload class
        $config['upload_path'] = $upload_dir . '/' . $folder . '/';
        $config['allowed_types'] = $this->config->item('acceptable_files');
        $config['max_size']	= 1024;
        $arr = $_FILES['userfile']['type'];
        if($arr=='image/png'){
            $ext = ".png";
        }else if($arr=='image/jpeg'){
            $ext = ".jpg";
        }else {
            $ext = ".jpg";
        }
        $config['file_name'] = 'avatar_'.time().$ext;
        $this->load->library('upload', $config);;
        // Output json as response
        if ( ! $this->upload->do_upload())
        {
            $json['status'] = 'error';
            $json['issue'] = $this->upload->display_errors('','');
        }
        else
        {
            $json['status'] = 'success';
            foreach($this->upload->data() as $k => $v)
            {
                $json[$k] = $v;
            }
            $json['file_url']= $config['upload_path'].$config['file_name'];
        }
        echo json_encode($json);
        exit();
    }
    //hàm xử lý xóa file upload
    public function delete_files($file_id){
        $this->load->model("mupload");
        if($this->mupload->delete_file($file_id)){
            $status = 'success';
            $msg = 'File successfully deleted';
        }else{
            $status = 'error';
            $msg = 'Something went wrong when deleting the file, please try again';
        }
        Echo json_encode(array('status' => $status, 'msg' => $msg));
    }



}
